package com.by.yz.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.common.MFGT;
import com.by.yz.entity.Account;
import com.by.yz.entity.AskBean;
import com.by.yz.entity.TechnologyTeam1;
import com.by.yz.entity.UserTest;
import com.by.yz.fragment.vehicleDiagnosisFragment;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.QustionRESFul;
import com.by.yz.loginUtil.TopicInfoRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.IsNetWork;
import com.by.yz.util.ToJSonObject;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by 123 on 2016/10/7.
 */
public class QuickAskActivity extends BaseActivity{

    private LinearLayout ask;
    private EditText askText;
    private Button ask_bt;
    List<AskBean> datas=new ArrayList<AskBean>();
    AskBean askBean1;
    String content;
    UserTest userTest;
    String img=null;
    String creater;
    String str;
    IsNetWork isNetWork;
    //TechnologyTeam1 technologyTeam;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quick_ask);

        initView();
        initDatas();

    }

    private void initDatas() {

    }

    private void initView() {
        setToolbarTitle("快速提问");
//        ask=(LinearLayout) findViewById(R.id.ask);
//        ask.setOnClickListener(this);

        askText=(EditText) findViewById(R.id.ask_edt);
        //askText.setOnClickListener(this);
        ask_bt=(Button) findViewById(R.id.ask_bt);
        ask_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                content = askText.getText().toString();
                if (content.length() < 15) {
                    Toast.makeText(QuickAskActivity.this, "字数不能小于15个字", Toast.LENGTH_SHORT).show();
                } else {
                    if (isNetWork.isNetworkAvailable(QuickAskActivity.this)){
                        final AccountRESTFul accountRESTFul=new AccountRESTFul();
                        accountRESTFul.get(new AccountRESTFul.IhttpCb(){
                            @Override
                            public void error(String error) {
                                Log.e("qwq","geng1"+error);
                            }

                            @Override
                            public void ok(JSONObject response) {
                                Date date=new Date(System.currentTimeMillis());//获取当前时间
                                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                                SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
                                sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
                                str = sdf1.format(date) + "T" + sdf2.format(date) + "Z";
                                creater=ToJSonObject.jsonToBean(response.toString(), Account.class).getLogin();
                                UserRESTFul userRESTFul=new UserRESTFul();
                                userRESTFul.getUser(creater, new UserRESTFul.UserPOSTCallBack() {
                                    @Override
                                    public void ok(JSONObject response) throws JSONException {
                                        userTest=ToJSonObject.jsonToBean(response.toString(),UserTest.class);
                                        img = userTest.getHeadimg();
                                        fabiao();
                                    }
                                    @Override
                                    public void error(String response) {
                                        Log.e("PCF-user-error","获取用户表失败"+response);
                                    }
                                });
                            }
                        });
                    }else{
                        //无网络连接情况
                        new AlertDialog.Builder(QuickAskActivity.this)
                                .setTitle("提示!")
                                .setIcon(android.R.drawable.ic_dialog_info)
                                .setMessage("检测到您还没开启网络，请开启")
                                .setNegativeButton("取消", null)
                                .setPositiveButton("开启",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog,
                                                                int which) {
                                                startActivity(new Intent(
                                                        Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
                                            }
                                        }).show();
                    }

                 }
            }
        });
    }
    void fabiao(){
        askBean1=new AskBean(null,img,creater,content,"",str,false);
        JSONObject jsonObject=ToJSonObject.beanToJSONObejct(askBean1);
        QustionRESFul qustionRESFul=new QustionRESFul();
        qustionRESFul.postQuestion(jsonObject, new QustionRESFul.MyPostCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                Log.e("qwq",response.toString());
                Toast.makeText(QuickAskActivity.this, "发表成功", Toast.LENGTH_SHORT).show();
                finish();
            }
            @Override
            public void error(String error) {
                Log.e("qwq","geng"+error);
                Toast.makeText(QuickAskActivity.this, "字数太多，请控制在255字内", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

